package com.hxci.zhongjiahao.controller;

import com.hxci.zhongjiahao.pojo.Course;
import com.hxci.zhongjiahao.servcie.CorseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller("zhongjiahaoCourseController")
@RequestMapping("course")
public class CourseController {
    @Autowired
    CorseService service;

    @RequestMapping("view")
    public String view(){
        return "zhongjiahao/zjhcourse";
    }

    @RequestMapping("add")
    public String add(){
        return "zhongjiahao/zjhadd";
    }
    
    //保存
    @ResponseBody
    @RequestMapping("save")
    public Integer save(Course course){
        System.out.println(course);
        Integer flag= service.save(course);
        return flag;
    }
    
    @ResponseBody
    @RequestMapping("query")
    public List<Course> query(Course  course){
        System.out.println("==========");
        List<Course>  list =  service.query(course);
        return list;
    }
    
    @ResponseBody
    @RequestMapping("del")
    public Integer del(String id){
        System.out.println("==========");
        Integer flag = service.del(id);
        return flag;
    }
    
    @RequestMapping("edit")
    public String edit(String id, Model model){
        Course course = service.queryBySno(id);
        model.addAttribute("course",course);
        return "zhongjiahao/zjhedit";
    }
    
    // 获取所有课程分类
    @ResponseBody
    @RequestMapping("categories")
    public List<String> categories() {
        return service.queryCategories();
    }
    
    // 根据分类查询课程
    @ResponseBody
    @RequestMapping("queryByCategory")
    public List<Course> queryByCategory(String category) {
        if (category == null || category.isEmpty()) {
            return service.queryAll();
        }
        return service.queryByCategory(category);
    }
    
    // 获取统计信息
    @ResponseBody
    @RequestMapping("statistics")
    public Map<String, Object> statistics() {
        Map<String, Object> stats = new HashMap<>();
        List<Course> allCourses = service.queryAll();
        
        // 课程总数
        stats.put("totalCourses", allCourses.size());
        
        // 按教师统计
        Map<String, Integer> teacherStats = new HashMap<>();
        for (Course course : allCourses) {
            String teacher = course.getTea();
            if (teacher != null && !teacher.isEmpty()) {
                teacherStats.put(teacher, teacherStats.getOrDefault(teacher, 0) + 1);
            }
        }
        stats.put("teacherStats", teacherStats);
        
        // 按分类统计
        Map<String, Integer> categoryStats = new HashMap<>();
        for (Course course : allCourses) {
            String category = course.getCategory();
            if (category != null && !category.isEmpty()) {
                categoryStats.put(category, categoryStats.getOrDefault(category, 0) + 1);
            }
        }
        stats.put("categoryStats", categoryStats);
        
        return stats;
    }
}